<?php

include ('autoloading_classes.include.php');
if ( in_array( strtolower( ini_get( 'magic_quotes_gpc' ) ), array( '1', 'on' ) ) )
{
    $_POST = array_map( 'stripslashes', $_POST );
    $_GET = array_map( 'stripslashes', $_GET );
    $_COOKIE = array_map( 'stripslashes', $_COOKIE );
}
/**
 * Description of ArticlesManager
 *
 * \author Gianmarco Nalin
 */
class ArticlesManager extends Manager {
    
    //! Query per recuperare l'articolo dato il suo id.
    const GET_ARTICLE_BY_ID = "SELECT * FROM articles WHERE (id = :id) AND (publication_timestamp <= CURRENT_TIMESTAMP) AND (seo_title = :seo_title);";
    
    //! Query per recuperare l'articolo dato il suo id (Administration Version).
    const GET_ARTICLE_BY_ID_ADMIN = "SELECT * FROM articles WHERE id = :id;";
    
    //! Query per recuperare tutti gli articoli pubblicati (publication_timestamp <= CURRENT_TIMESTAMP).
    const GET_PUBLICATED_ARTICLES = "SELECT * FROM articles WHERE publication_timestamp <= CURRENT_TIMESTAMP ORDER BY publication_timestamp DESC;";
    
    //! Query per salvare un nuovo articolo.
    const SAVE_ARTICLE = "INSERT INTO articles (title, abstract, content, author, news_image, article_image, links, is_fdg, is_fdr, publication_timestamp, seo_title, creator) VALUES (:title, :abstract, :content, :author, :news_image, :article_image, :links, :is_fdg, :is_fdr, :publication_timestamp, :seo_title, :creator);";
    
    //! Query per salvare le modifiche ad un articolo già esistente.
    const UPDATE_ARTICLE = "UPDATE articles SET title = :title, abstract = :abstract, content = :content, author = :author, news_image = :news_image, article_image = :article_image, links = :links, is_fdg = :is_fdg, is_fdr = :is_fdr, publication_timestamp = :publication_timestamp WHERE id = :id;";
    
    //! Query per eliminare un articolo dato il suo id.
    const DELETE_ARTICLE = "DELETE FROM articles WHERE id = :id;";
    
    //! Query per recuperare tutti gli articoli che riguardano la Festa dei Giovani.
    const GET_FDG_ARTICLES = "SELECT * FROM articles WHERE (is_fdg = 1) AND (publication_timestamp <= CURRENT_TIMESTAMP) ORDER BY publication_timestamp DESC;";
    
    //! Query per recuperare tutti gli articoli che riguardano la Festa dei Ragazzi.
    const GET_FDR_ARTICLES = "SELECT * FROM articles WHERE (is_fdr = 1) AND (publication_timestamp <= CURRENT_TIMESTAMP) ORDER BY publication_timestamp DESC;";
    
    //! Metodo per salvare un nuovo articolo
    /*!
     * \param a Nuovo articolo passato per riferimento. Se il salvataggio va a buon fine al ritorno nel campo id dell'articolo viene impostato l'id assegnato.
     * \return TRUE se l'articolo è stato salvato, FALSE altrimenti.
     */
    public function save_article(&$a) {
        $stmt = $this->connection->prepare(self::SAVE_ARTICLE);
        echo '0';
        $stmt->bindParam(':title', $a->title, PDO::PARAM_STR);
        echo '1';
        $stmt->bindParam(':abstract', $a->abstract);
        echo '2';
        $stmt->bindParam(':content', $a->content);
        echo '3';
        $stmt->bindParam(':author', $a->author);
        echo '4';
        $stmt->bindValue(':news_image', $a->news_image, PDO::PARAM_STR);
        echo '5';
        $stmt->bindValue(':article_image', $a->article_image, PDO::PARAM_STR);
        echo '6';
        $stmt->bindParam(':links', $a->links, PDO::PARAM_STR);
        echo '7';
        $stmt->bindValue(':is_fdg', 1, PDO::PARAM_INT);
        echo '8';
        $stmt->bindValue(':is_fdr', 1, PDO::PARAM_INT);
        echo '9';
        $str_date = $a->publication_timestamp->format('Y-m-d H:i:s');
        $stmt->bindParam(':publication_timestamp', $str_date, PDO::PARAM_STR);
        echo '0';
        $stmt->bindParam(':seo_title', $a->seo_title, PDO::PARAM_STR);
        echo '1';
        $stmt->bindParam(':creator', $a->creator, PDO::PARAM_STR);
        echo '2';
        $stmt->execute();
        $a->id = $this->connection->lastInsertid();
        return $stmt->rowCount() > 0;
    }
    
    //! Metodo per recuperare un articolo dato il suo id
    /*!
     * \sa Article
     * \param id Identificatore dell'articolo.
     * \param admin=FALSE TRUE se si desidera recuperare l'articolo in modalità amministratore: l'articolo viene restitutito anche se il timestamp di pubblicazione è maggiore del CURRENT_TIMESTAMP.
     * \return Se l'articolo esiste viene restituito un oggetto Article contenente tutte le informazioni ad esso relative, altrimenti null. 
     */
    public function get_article_by_id($id, $seo = null, $admin = false) {
        if ($admin) {
            $stmt = $this->connection->prepare(self::GET_ARTICLE_BY_ID_ADMIN);
            $stmt->execute(array('id' => $id));
        } else {
            $stmt = $this->connection->prepare(self::GET_ARTICLE_BY_ID);
            $stmt->execute(array('id' => $id, 'seo_title' => $seo));
        }
        $article = null;
        if ($a = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $article = new Article();
            foreach ($a as $key => $value) {
                if (($key == 'publication_timestamp') || ($key == 'creation_timestamp')) {
                    $article->{$key} = new DateTime($value);
                } else if (($key == 'is_fdg') || ($key == 'is_fdr')) {
                    $article->{$key} = ($value == 1) ? TRUE : FALSE;
                } else {
                    $article->{$key} = $value;
                }
                
            }
        }
        
        return $article;
    }
    
    //! Metodo per salvare le modifiche ad un articolo già esistente
    /*!
     * \param a Nuovo articolo passato per riferimento. Se il salvataggio va a buon fine al ritorno nel campo id dell'articolo viene impostato l'id assegnato.
     * \return TRUE se l'articolo è stato aggiornato, FALSE altrimenti
     */
    public function update_article($a) {
        $stmt = $this->connection->prepare(self::UPDATE_ARTICLE);
        $stmt->bindParam(':id', $a->id, PDO::PARAM_INT);
        $stmt->bindParam(':title', $a->title, PDO::PARAM_STR);
        $stmt->bindParam(':abstract', $a->abstract);
        $stmt->bindValue(':content', $a->content);
        $stmt->bindParam(':author', $a->author);
        $stmt->bindParam(':news_image', $a->news_image, PDO::PARAM_STR);
        $stmt->bindParam(':article_image', $a->article_image, PDO::PARAM_STR);
        $stmt->bindParam(':links', $a->links, PDO::PARAM_STR);
        $stmt->bindValue(':is_fdg', ($a->is_fdg) ? 1 : 0, PDO::PARAM_INT);
        $stmt->bindValue(':is_fdr', ($a->is_fdr) ? 1 : 0, PDO::PARAM_INT);
        $str_date = $a->publication_timestamp->format('Y-m-d H:i:s');
        $stmt->bindParam(':publication_timestamp', $str_date, PDO::PARAM_STR);
        $stmt->execute();
        return $stmt->rowCount() > 0;
    }
    
    //! Metodo per salvare un nuovo articolo
    /*!
     * \param id Identificatore dell'articolo da eliminare.
     * \return TRUE se l'articolo è stato aliminato, FALSE altrimenti.
     */
    public function delete_article($id) {
        $stmt = $this->connection->prepare(self::DELETE_ARTICLE);
        $stmt->execute(array('id' => $id));
        return $stmt->rowCount() > 0;
    }
    
    //! Metodo per recuperare tutti gli articoli pubblicati.
    /*!
     * \sa Article
     * \return Un array di Article.
     */
    public function get_publicated_articles() {
        $stmt = $this->connection->prepare(self::GET_PUBLICATED_ARTICLES);
        $stmt->execute();
        $articles = array();
        $article = null;
        while ($a = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $article = new Article();
            foreach ($a as $key => $value) {
                if (($key == 'publication_timestamp') || ($key == 'creation_timestamp')) {
                    $article->{$key} = new DateTime($value);
                } else if (($key == 'is_fdg') || ($key == 'is_fdr')) {
                    $article->{$key} = ($value == 1) ? TRUE : FALSE;
                } else {
                    $article->{$key} = $value;
                }
            }
            array_push($articles, $article);
        }
        return $articles;
    }
    
    //! Metodo per recuperare tutti gli articoli pubblicati che riguardano la Festa dei Giovani.
    /*!
     * \sa Article
     * \return Un array di Article.
     */
    public function get_publicated_fdg_articles() {
        $stmt = $this->connection->prepare(self::GET_FDG_ARTICLES);
        $stmt->execute();
        $articles = array();
        $article = null;
        while ($a = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $article = new Article();
            foreach ($a as $key => $value) {
                if (($key == 'publication_timestamp') || ($key == 'creation_timestamp')) {
                    $article->{$key} = new DateTime($value);
                } else if (($key == 'is_fdg') || ($key == 'is_fdr')) {
                    $article->{$key} = ($value == 1) ? TRUE : FALSE;
                } else {
                    $article->{$key} = $value;
                }
            }
            array_push($articles, $article);
        }
        return $articles;
    }
    
    //! Metodo per recuperare tutti gli articoli pubblicati che riguardano la Festa dei Ragazzi.
    /*!
     * \sa Article
     * \return Un array di Article.
     */
    public function get_publicated_fdr_articles() {
        $stmt = $this->connection->prepare(self::GET_FDR_ARTICLES);
        $stmt->execute();
        $articles = array();
        $article = null;
        while ($a = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $article = new Article();
            foreach ($a as $key => $value) {
                if (($key == 'publication_timestamp') || ($key == 'creation_timestamp')) {
                    $article->{$key} = new DateTime($value);
                } else if (($key == 'is_fdg') || ($key == 'is_fdr')) {
                    $article->{$key} = ($value == 1) ? TRUE : FALSE;
                } else {
                    $article->{$key} = $value;
                }
            }
            array_push($articles, $article);
        }
        return $articles;
    }
}

?>
